package flexo.gui;

import flexo.math.Coords;
import flexo.math.Rect;
import flexo.math.Vector;
import flexo.scene.Node;

public abstract class Widget extends flexo.scene.ExclusiveEntity
{
	protected flexo.math.Rect rect;
	protected Coords size;
	protected String name;
	
	Coords getSize()
	{
		return size;
	}
	
	public String getName()
	{
		return name;
	}
	
	flexo.scene.Node getNode()
	{
		return this.exclusiveNode;
	}		
	
	public Widget(String name, Coords size)
	{
		super(flexo.scene.Entity.Type.OTHER);
		this.size = size;
		rect = new Rect();
		this.name = name;
	}		
	
	private void updateRect(Coords position)
	{							
		rect.topLeft.x = position.x - size.x * 0.5f;
		rect.topLeft.y = position.y + size.y * 0.5f;
		
		rect.bottomRight.x = position.x + size.x * 0.5f;
		rect.bottomRight.y = position.y - size.y * 0.5f;
	}

	public boolean isMouseOver(float x, float y)
	{
		return rect.isInside(x, y);
	}	
	
	@Override
	public boolean attachTo(Node n)
	{
		boolean result = super.attachTo(n);
		if (result)
		{
			Vector absPos = n.getAbsolutePosition();
			updateRect(new Coords(absPos.x,absPos.y));
		}
		return result;
	}
}
